package net.sourceforge.krama.tag.widget;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.krama.css.CSSDiv;
import net.sourceforge.krama.css.CSSTemplate;
import net.sourceforge.krama.exception.InvalidOnClickCall;
import net.sourceforge.krama.html.child.HTMLLabel;
import net.sourceforge.krama.html.child.HTMLLink;
import net.sourceforge.krama.listener.TEventListener;
import net.sourceforge.krama.listener.TMouseListener;
import net.sourceforge.krama.util.TUtil;


public class TLabel extends AbstractT {

	private String color;
	private String text;
	private String size;
	private String style;
	private String html = "";

	private CSSTemplate cssLabel = new CSSTemplate();
	private HTMLLabel htmlLabel = new HTMLLabel();
	
	private String name;
	private String id;
	private String parentName;
	private String fonction;
	private boolean drag = false;
	
	private List<TEventListener> listenerList = new ArrayList<TEventListener>();

	public TLabel(String text) throws IOException {
		this.text = text;
	}
	
	public TLabel(String name, String id) throws IOException {
		if (name != null) {
			this.name = "krama-" + name;
			htmlLabel.setName(this.name);
		}
		if (id != null) {
			this.id = id;
			htmlLabel.setId("krama-"+this.id);
		}
	}

	public String getColor() {
		return color;
	}

	public void setColor(String color) {
		this.color = color;
		cssLabel.setColor(color);
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
		cssLabel.setSize(size);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		if (name != null) {
			this.name = "krama-" + name;
			htmlLabel.setName(this.name);
		}
	}

	@Override
	public void draw() {

		this.html = htmlLabel.starDraw();
		this.html += text;
		this.html += htmlLabel.endDraw();
	}

	public String getHtml() {
		return this.html;
	}

	public void addTMouseListener(TMouseListener mouseListener, String id)
			throws IllegalArgumentException {

		listenerList.add(mouseListener);
		fonction = mouseListener.onClick();
		fonction += ", 'krama-" + id + "'";
		htmlLabel.addClick(fonction);
	}

	public String getCSS() {
		if (name != null) {
			return cssLabel.getCSS("."+parentName + " ." + name);
		} else
			return "";
	}

	public String getParentName() {
		return parentName;
	}

	public void setParentName(String parentName) {
		this.parentName = parentName;
	}

	
	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
		cssLabel.setStyle(style);
	}

	public void setDrag(boolean drag) {
		
		this.drag = drag;
		htmlLabel.setDrag(true);
		
		TUtil.javascriptShouldBeAvailable = true;
		TUtil.domDragShouldBeAvailable = true;
		
		cssLabel.setStyle((style!=null)?style:""+" position:relative;");
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		if (id != null) {
			this.id = id;
			htmlLabel.setId("krama-"+this.id);
		}
	}
}
